<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Push extends MY_Controller
{
	private $push_status;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->push_status = array('进行中', '已完成');
	}

	public function index()
	{
		$d['coins'] = $this->d->rows(array('table' => 'coin'));
		$d['status'] = $this->push_status;
		$this->view('push', $d);
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$status = $this->input->post('status');
		$username = $this->input->post('username');
		$start = $this->input->post('start');
		$end = $this->input->post('end');
		$coin_id = $this->input->post('coin_id');
		$where[1] = 1;

		if ($username) {
			$user = $this->db->where('username', $username)->get('user')->row_array();
			$where['user_id'] = $user['id'];
		}

		if ($start) {
			$where['add_time>='] = strtotime($start);
		}

		if ($end) {
			$where['add_time<='] = strtotime($end);
		}

		if ($id) {
			$where['id'] = $id;
		}

		if ($coin_id) {
			$where['coin_id'] = $coin_id;
		}

		if ((0 <= $status) && ($status != NULL)) {
			$where['status'] = $status;
		}

		$result['total'] = $this->db->where($where)->count_all_results('push');
		$start = ($page - 1) * $rows;
		$list = $this->db->where($where)->limit($rows, $start)->get('push')->result_array();

		foreach ($list as $k => $v) {
			$list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
			$user = $this->db->where('id', $v['user_id'])->get('user')->row_array();
			$list[$k]['username'] = $user['username'];
			$list[$k]['status_name'] = $this->push_status[$v['status']];
			$list[$k]['coin_name'] = $this->d->field(array('table' => 'coin', 'where' => 'id=' . $v['coin_id'], 'field' => 'cn_name'));
		}

		$result['rows'] = $list;
		echo json_encode($result);
	}

	public function add()
	{
		$d['coins'] = $this->db->get('coin')->result_array();
		$this->load->view('push_add', $d);
	}

	public function insert()
	{
		$user_id = intval($this->input->post('user_id'));
		$to_uid = $this->input->post('to_uid');
		$to_name = $this->input->post('to_name');
		$coin_id = intval($this->input->post('coin_id'));
		$amount = num($this->input->post('amount'));
		$total = num($this->input->post('total'));
		$data = array('user_id' => $user_id, 'to_uid' => $to_uid, '$to_name' => $to_name, 'coin_id' => $coin_id, 'amount' => $amount, 'total' => $total, 'add_time' => time());
		$rs = $this->db->insert('push', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('push')->row_array();
		$d['coins'] = $this->db->get('coin')->result_array();
		$this->load->view('push_edit', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$user_id = intval($this->input->post('user_id'));
		$to_uid = $this->input->post('to_uid');
		$to_name = $this->input->post('to_name');
		$coin_id = intval($this->input->post('coin_id'));
		$amount = num($this->input->post('amount'));
		$total = num($this->input->post('total'));
		$data = array('user_id' => $user_id, 'to_uid' => $to_uid, '$to_name' => $to_name, 'coin_id' => $coin_id, 'amount' => $amount, 'total' => $total, 'add_time' => time());
		$rs = $this->db->where('id', $id)->update('push', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('push');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}

	public function set_status()
	{
		$id = intval($this->input->post('id'));
		$status = intval($this->input->post('status'));
		$this->db->where('id', $id);
		$this->db->set('status', $status);

		if ($this->db->update('push')) {
			ajax_return(0, '操作成功');
		}
		else {
			ajax_return(1, '操作失败');
		}
	}
}

?>
